import { Component } from "@angular/core";
import {
  NzModalService,
  NzModalSubject,
  NzNotificationService
} from "ng-zorro-antd";
import { Config } from "./../../service/config";
import { HttpServcie } from "../../service/http.service";
import { MaintenancePlanBean } from "./bean/maintenancePlanBan";
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from "@angular/forms";

@Component({
  selector: "maintenance-plan",
  templateUrl: "maintenancePlan.html",
  styleUrls: ["maintenancePlan.scss"]
})
export class MaintenancePlan {
  //表格
  config: any = Config;
  data: any = [];
  modal: NzModalSubject;
  //多选框
  allChecked: any = false;
  //多选框
  indeterminate: any = false;
  total: string = "0";
  maintenancePlanBean: MaintenancePlanBean = new MaintenancePlanBean();
  // 查询面板
  sysUser: any;
  panels = {
    active: true,
    name: "查询",
    disabled: false,
    childPanel: [
      {
        active: false,
        name: "查询"
      }
    ]
  };
  constructor(
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private nsub: NzModalSubject,
    private modalService: NzModalService,
    private fb: FormBuilder
  ) {
    this.sysUser = JSON.parse(window.localStorage.getItem("user"));
    this.maintenancePlanBean.partyId = this.sysUser.partyId;
    this.maintenancePlanBean.pageSize = 10;
    this.maintenancePlanBean.currentPage = 1;
    this.query();
  }

  //全选
  checkAll(value) {
    if (value) {
      this.data.forEach(data => {
        if (!data.disabled) {
          data.checked = true;
        }
      });
    } else {
      this.data.forEach(data => (data.checked = false));
    }
  }

  //table加载数据时
  displayDataChange($event) {
    this.data = $event;
    this.refreshStatus();
  }

  //重置选择框
  refreshStatus() {
    const allChecked = this.data.every(
      value => value.disabled || value.checked
    );
    const allUnChecked = this.data.every(
      value => value.disabled || !value.checked
    );
    this.allChecked = allChecked;
    this.indeterminate = !allChecked && !allUnChecked;
  }
  //查询
  query() {
    this.httpServcie
      .maintenanceList(this.maintenancePlanBean)
      .subscribe(data => {
        if (data.status == "200") {
          if (data.data) {
            this.data = data.data.list;
            this.total = data.data.total;
          } else {
            this.data = [];
          }
        } else {
          this._notification.create("error", "错误提示", data.message);
        }
      });
  }

  //分页
  nzPageSizeChange(event) {
    this.maintenancePlanBean.pageSize = event;
    this.maintenancePlanBean.currentPage = 1;
    this.query();
  }

  //输入框的方法
  nzPageIndexChange(event) {
    this.maintenancePlanBean.currentPage = event;
    this.query();
  }

  //当前页
  nzPageIndexClickChange(event) {
    this.maintenancePlanBean.currentPage = event;
    this.query();
  }

  //添加
  add(title, content, footer) {
    this.modal = this.modalService.open({
      title: "添加",
      width: 600,
      content: content,
      showConfirmLoading: true
    });
  }

  delete() {
    const deleteList: Array<string> = [];
    this.data.forEach(element => {
      if (element.checked) {
        deleteList.push(element.id + "");
      }
    });

    if (deleteList.length == 0) {
      // 异常
      this._notification.create("warning", "警告提示", "请选中一条数据！");
      return;
    }
    if (deleteList.length > 1) {
      // 异常
      this._notification.create("warning", "警告提示", "只能选中一条数据！");
      return;
    }
    let thiss = this;
    this.modalService.confirm({
      title: "您是否确认要删除这项内容",
      content: "<b>一旦删除，无法还原</b>",
      onOk() {
        let ob = thiss.httpServcie.maintenanceDeleteById(deleteList[0]); //批量删除
        ob.subscribe(
          (data) => {
            if (data.status = 200) {
              thiss._notification.create("success", "成功", "删除成功！");
              thiss.maintenancePlanBean.currentPage = 1;
              thiss.query();
            } else {
              this._notification.create("error", "错误提示", "删除失败！");
            }

          },
          error => {
            this._notification.create("error", "错误提示", "删除失败！");
          }
        );
      },
      onCancel() { }
    });
  }

  //添加返回
  addCall(event) {
    this.modal.destroy();
    this.query();
  }

  /**
   * 返回一个集合
   * @param para
   * @returns {string[]}
   */
  showImageList(para) {
    if (para) {
      return para.split(",");
    }
    return [];
  }
}
